package com.hxzy.driver.controller;

import com.hxzy.common.util.R;
import com.hxzy.driver.entity.Driver;
import com.hxzy.driver.service.DriverService;
import com.hxzy.drvier.req.CreateDriverFaceModelReq;
import com.hxzy.drvier.req.UpdateDriverAuthReq;
import com.hxzy.drvier.resp.DriverResp;
import com.hxzy.drvier.req.RegisterNewDriverReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/12/12 11:30
 * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机模块Web接口")
public class DriverController {


    /**
     * 司机业务逻辑
     */
    @Autowired
    private DriverService  driverService;

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R<Map<String,String>> registerNewDriver(@RequestBody @Valid RegisterNewDriverReq req) {
        //https://api.weixin.qq.com/sns/jscode2session?appid=wxdbe6faab1d5c0cd6&secret=ddd1e16808f0381fb9cee1e2da47b9ad&js_code=0e3WQ9100NpIbR1WC1400muz7e1WQ91F&grant_type=authorization_code
        String userId = driverService.registerNewDriver(req);

        Map<String,String> map=new HashMap<>();
        map.put("userId",userId);
        return  R.ok(map);
    }


    @GetMapping(value = "/info/{driverId}")
    @Operation(summary = "获取司机信息")
    public R<DriverResp>  getDriverInfo(@PathVariable(value = "driverId") long driverId){
        Driver driver = this.driverService.getById(driverId);

        DriverResp  resp=new DriverResp();
        BeanUtils.copyProperties(driver,resp);
        return R.ok(resp);
    }

    @Operation(summary = "更新实名认证信息")
    @PostMapping(value = "/updateDriverAuth")
    public R<Integer> updateDriverAuth(@RequestBody @Valid UpdateDriverAuthReq req){
        Driver driver = this.driverService.getById(req.getDriverId());
        if(driver==null){
            return R.error("查询不到数据");
        }

        BeanUtils.copyProperties(req,driver);
        driver.setRealAuth(3);
        boolean success = this.driverService.updateById(driver);
        return R.ok(success?1:0);
    }

    @PostMapping("/createDriverFaceModel")
    @Operation(summary = "创建司机人脸模型归档")
    public R<Map<String,String>> createDriverFaceModel(@RequestBody @Valid CreateDriverFaceModelReq req) {
        String result = driverService.createDriverFaceModel(req.getDriverId(), req.getPhoto());

        Map<String,String> map=new HashMap<>();
        map.put("result",result);
        return R.ok(map);
    }

}
